package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.TBaseStorageAreaLocation;
import com.ruoyi.system.domain.TMiStock;
import com.ruoyi.system.mapper.TBaseStorageAreaLocationMapper;
import com.ruoyi.system.mapper.TMiStockMapper;
import com.ruoyi.system.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class WarehouseServiceImpl implements WarehouseService {
    @Autowired
    private TBaseStorageAreaLocationMapper tBaseStorageAreaLocationMapper;

    @Autowired
    private TMiStockMapper mapper;
    @Override
    public Map<String, Map<String, Map<String,List<TBaseStorageAreaLocation>>>> getWarehouseData() {
//        Map<String, Map<String, List<TBaseStorageAreaLocation>>> warehouseData = new LinkedHashMap<>();
        Map<String, Map<String, Map<String,List<TBaseStorageAreaLocation>>>> warehouseData = new LinkedHashMap<>();
        List<TBaseStorageAreaLocation> tBaseStorageAreaLocations = tBaseStorageAreaLocationMapper.selectList();

        // 批量查询获取所有 TMiStock 记录
//        List<TMiStock> tMiStockList = mapper.selectTMiStockList(new TMiStock()); // 假设此方法检索所有 TMiStock 记录
//        Map<String, TMiStock> stockMap = tMiStockList.stream()
//                .collect(Collectors.toMap(TMiStock::getLocationId, Function.identity()));

        for (TBaseStorageAreaLocation location : tBaseStorageAreaLocations) {
            String substring = location.getLocationId().substring(6);
            location.setLocationId(substring);
            // 使用 Map 进行快速查找
//            TMiStock tMiStocks = stockMap.get(location.getLocationId());
//            if (tMiStocks == null) {
//                location.setSTATUS("1");
//            } else {
//                location.setSTATUS("0");
//            }
            String row = location.getRow();
            String cln = location.getCln();
            String layer = location.getLayer();
            if (!warehouseData.containsKey(row)) {
                warehouseData.put(row, new LinkedHashMap<>());
            }
//            Map<String, List<TBaseStorageAreaLocation>> areaMap = warehouseData.get(row);
            Map<String, Map<String, List<TBaseStorageAreaLocation>>> areaMap = warehouseData.get(row);
            if (!areaMap.containsKey(cln)) {
//                areaMap.put(cln, new ArrayList<>());
                areaMap.put(cln,new LinkedHashMap<>());
            }
            Map<String, List<TBaseStorageAreaLocation>> stringListMap = areaMap.get(cln);
            if(!stringListMap.containsKey(layer)){
                stringListMap.put(layer,new ArrayList<>());
            }
//            areaMap.get(cln).add(location);
            stringListMap.get(layer).add(location);
        }
        return warehouseData;
    }

    @Override
    public AjaxResult getList(TBaseStorageAreaLocation location) {
//        TMiStock tMiStock = new TMiStock();
//        tMiStock.setLocationId(location.getLocationId());
//        TMiStock tMiStocks = mapper.selectOne(tMiStock);
//        if(tMiStocks==null){
//            return AjaxResult.error("暂无库存");
//        }
        TBaseStorageAreaLocation tBaseStorageAreaLocation = tBaseStorageAreaLocationMapper.selectTBaseStorageAreaLocationByLocationId(location.getLocationId());
        if(tBaseStorageAreaLocation.getSTATUS().equals("0")||tBaseStorageAreaLocation.getSTATUS().equals("1")){
            return AjaxResult.error("暂无库存");
        }else {
            TMiStock tMiStock = new TMiStock();
            tMiStock.setLocationId(location.getLocationId());
            TMiStock tMiStocks = mapper.selectOne(tMiStock);
            return AjaxResult.success(tMiStocks);
        }

    }
}

